/*
 * Copyright (c) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.client.sample.gan;

import com.google.api.services.gan.Gan;
import com.google.api.services.gan.model.CcOffer;
import com.google.api.services.gan.model.Advertiser;
import com.google.api.services.gan.model.Event;
import com.google.api.services.gan.model.Publisher;
import com.google.api.services.samples.shared.cmdline.oauth2.LocalServerReceiver;
import com.google.api.services.samples.shared.cmdline.oauth2.OAuth2ClientCredentials;
import com.google.api.services.samples.shared.cmdline.oauth2.OAuth2Native;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.ApacheHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;

import java.util.List;

/**
 * Queries the GAN API for credit card, advertiser, publisher and event data.
 * See for GAN API documentation:
 * <a href="https://code.google.com/apis/gan/index.html">Google Affiliate Network Documentaiton</a>
 * @author namos@google.com (Ryan Oman)
 */
public class GanSample {

  private static final String SCOPE = "https://www.googleapis.com/auth/gan.readonly";

  public static void main(String[] args) throws Exception {
    OAuth2ClientCredentials.errorIfNotSpecified();

    Params params = Params.parse(args);

    if (params == null) {
      Params.printUsage();
      return;
    }

    HttpTransport transport = new ApacheHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    HttpRequestInitializer init = OAuth2Native.authorize(
        transport, jsonFactory, new LocalServerReceiver(), null, "google-chrome",
        OAuth2ClientCredentials.CLIENT_ID, OAuth2ClientCredentials.CLIENT_SECRET, SCOPE);

    switch (params.getType()) {
      case CC_OFFERS:
        System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        System.out.println("Credit Card Offers Api");
        System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        queryCcOfferApi(params, transport, jsonFactory, init);
        break;
      case EVENTS:
        System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        System.out.println("Event Api");
        System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        queryEventApi(params, transport, jsonFactory, init);
        break;
      case ADVERTISERS:
        System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        System.out.println("Advertiser Api List");
        System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        queryAdvertiserApi(params, transport, jsonFactory, init);
        break;
      case PUBLISHERS:
        System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        System.out.println("Publisher Api List");
        System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        queryPublisherApi(params, transport, jsonFactory, init);
        break;
      case ADVERTISER:
        System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        System.out.println("Advertiser Api Get/Lookup");
        System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        queryAdvertiserLookupApi(params, transport, jsonFactory, init);
        break;
      case PUBLISHER:
        System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        System.out.println("Publisher Api Get/Lookup");
        System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        queryPublisherLookupApi(params, transport, jsonFactory, init);
        break;
    }
  }

  private static void queryAdvertiserLookupApi(
      Params params, HttpTransport transport, JsonFactory jsonFactory, HttpRequestInitializer init)
      throws Exception {
    // Set up server
    Gan server = new Gan(transport, init, jsonFactory);

    // Set up request
    Gan.Advertisers.Get request = server.advertisers.get(params.getRole(), params.getRoleId());
    if (params.hasLookupId()) {
      request.setAdvertiserId(params.getLookupId());
    }

    // Execute request
    Advertiser result  = request.execute();
    if (result == null) {
      System.out.println("No Result Returned!");
    } else {
      System.out.println(result);
    }
  }

  private static void queryAdvertiserApi(
      Params params, HttpTransport transport, JsonFactory jsonFactory, HttpRequestInitializer init)
      throws Exception{
    // Set up server
    Gan server = new Gan(transport, init, jsonFactory);

    // Set up request
    Gan.Advertisers.List request = server.advertisers.list(params.getRole(), params.getRoleId());
    request.setAdvertiserCategory("travel");

    // Execute request
    List<Advertiser> results = request.execute().getItems();
    if (results == null) {
      System.out.println("No Results Returned!");
    } else {
      for (Advertiser a : results) {
        System.out.println(a);
      }
    }
  }

  private static void queryCcOfferApi(
      Params params, HttpTransport transport, JsonFactory jsonFactory, HttpRequestInitializer init)
      throws Exception {
    // Set up server
    Gan server = new Gan(transport, init, jsonFactory);

    // Set up request
    Gan.CcOffers.List request = server.ccOffers.list(params.getRoleId());

    // Execute request
    List<CcOffer> results = request.execute().getItems();
    if (results == null) {
      System.out.println("No Results Returned!");
    } else {
      for (CcOffer c : results) {
        System.out.println(c);
      }
    }
  }

  private static void queryEventApi(
      Params params, HttpTransport transport, JsonFactory jsonFactory, HttpRequestInitializer init)
      throws Exception{
    // Set up server
    Gan server = new Gan(transport, init, jsonFactory);

    // Set up request
    Gan.Events.List request = server.events.list(params.getRole(), params.getRoleId());
    request.setEventDateMin("2011-08-11T04:11:42.000Z");

    // Execute request
    List<Event> results = request.execute().getItems();
    System.out.println("Not filtered: ");
    if (results == null) {
      System.out.println("No Results Returned!");
    } else {
      for (Event e : results) {
        System.out.println("Advertiser ID: " + e.getAdvertiserId() + " Order ID: " + e.getOrderId()
                           + " Event Date: " + e.getEventDate());
      }
    }
  }

  private static void queryPublisherApi(
      Params params, HttpTransport transport, JsonFactory jsonFactory, HttpRequestInitializer init)
      throws Exception {
    // Set up server
    Gan server = new Gan(transport, init, jsonFactory);

    // Set up request
    Gan.Publishers.List request = server.publishers.list(params.getRole(), params.getRoleId());
    request.setPublisherCategory("community_and_content");

    // Execute request
    List<Publisher> results = request.execute().getItems();
    if (results == null) {
      System.out.println("No Results Returned!");
    } else {
      for (Publisher p : results) {
        System.out.println(p);
      }
    }
  }

  private static void queryPublisherLookupApi(
      Params params, HttpTransport transport, JsonFactory jsonFactory, HttpRequestInitializer init)
      throws Exception {
    // Set up server
    Gan server = new Gan(transport, init, jsonFactory);

    // Set up request
    Gan.Publishers.Get request = server.publishers.get(params.getRole(), params.getRoleId());
    if (params.hasLookupId()) {
      request.setPublisherId(params.getLookupId());
    }

    // Execute request
    Publisher result = request.execute();
    if (result == null) {
      System.out.println("No Result Returned!");
    } else {
      System.out.println(result);
    }
  }
}
